import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:my_salary/pages/settings/locale/locale_controller.dart';
import 'package:my_salary/pages/settings/theme/theme_controller.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';

import 'package:my_salary/route/route_utils.dart';
import 'package:my_salary/route/routes.dart';

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return Consumer2<ThemeController, LocaleController>(
      builder: (context, themeController, localeController, _) {
        return OKToast(
          child: ScreenUtilInit(
            designSize: _designSize,
            builder: (context, child) {
              return MaterialApp(
                debugShowCheckedModeBanner: false,
                // 控制主题模式
                theme: _lightTheme(),
                darkTheme: _darkTheme(),
                themeMode: themeController.themeMode,

                // 国际化配置
                locale: localeController.locale,
                localizationsDelegates: const [
                  AppLocalizations.delegate,
                  GlobalMaterialLocalizations.delegate,
                  GlobalWidgetsLocalizations.delegate,
                  GlobalCupertinoLocalizations.delegate,
                ],
                supportedLocales: const [
                  Locale('en'),
                  Locale('zh'),
                ],
                localeResolutionCallback: (locale, supportedLocales) {
                  if (locale == null) return supportedLocales.first;
                  for (var supportedLocale in supportedLocales) {
                    if (supportedLocale.languageCode == locale.languageCode) {
                      return supportedLocale;
                    }
                  }
                  return supportedLocales.first;
                },
                // 路由配置
                navigatorObservers: [RouteUtils.routeObserver],
                navigatorKey: RouteUtils.navigatorKey,
                onGenerateRoute: Routes.generateRoute,
                initialRoute: RoutePath.home,
              );
            },
          ),
        );
      },
    );
  }

  ThemeData _lightTheme() {
    return ThemeData(
      useMaterial3: true,
      brightness: Brightness.light,
      tabBarTheme: const TabBarTheme(dividerColor: Colors.transparent),
    );
  }

  ThemeData _darkTheme() {
    return ThemeData(
      useMaterial3: true,
      brightness: Brightness.dark,
      tabBarTheme: const TabBarTheme(dividerColor: Colors.transparent),
    );
  }
}

/// 设计尺寸
Size get _designSize {
  final firstView = WidgetsBinding.instance.platformDispatcher.views.first;
  final logicalShortestSide =
      firstView.physicalSize.shortestSide / firstView.devicePixelRatio;
  final logicalLongestSide =
      firstView.physicalSize.longestSide / firstView.devicePixelRatio;
  const scaleFactor = 1;
  return Size(
    logicalShortestSide * scaleFactor,
    logicalLongestSide * scaleFactor,
  );
}
